<template>
  <div>
    <el-table
      ref="tableRef"
      v-loading="tableOptions.loading"
      :border="true"
      :data="tableOptions.dataList"
      :cell-style="tableStyle.cellStyle"
      :header-cell-style="tableStyle.headerCellStyle"
      :height="tableOptions.height"
      @selection-change="handleSelectionChange"
      @row-click="handleRowClick"
      @sort-change="handleSortChange"
    >
      <el-table-column type="index" label="序号" width="55" />
      <el-table-column
        prop="initiatorId"
        label="申请人"
        show-overflow-tooltip
        width="120"
      >
        <template #default="scope">
          {{
            scope.row.runFlow && scope.row.runFlow.initiatorName
              ? scope.row.runFlow.initiatorName
              : scope.row.runFlow.initiatorId
          }}
        </template>
      </el-table-column>
      <el-table-column
        prop="orderInstCode"
        label="工单编号"
        show-overflow-tooltip
        width="210"
      />
      <el-table-column
        prop="defFlowId"
        label="流程名称"
        show-overflow-tooltip
        width="160"
      >
        <template #default="scope">
          {{
            scope.row.runFlow && scope.row.runFlow.flowName
              ? scope.row.runFlow.flowName
              : scope.row.flowInstId
          }}
        </template>
      </el-table-column>
      <el-table-column
        prop="flowInstId"
        label="流程实例"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        prop="defNodeId"
        label="节点名称"
        show-overflow-tooltip
        width="160"
      >
        <template #default="scope">
          {{
            scope.row.runNode && scope.row.runNode.nodeName
              ? scope.row.runNode.nodeName
              : scope.row.defNodeId
          }}
        </template>
      </el-table-column>
      <el-table-column
        prop="jobName"
        label="任务名称"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        prop="jobTypeDesc"
        label="办理人类型"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        prop="roleName"
        label="办理角色"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        prop="userName"
        label="办理人"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        prop="startTime"
        label="开始时间"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        prop="endTime"
        label="办理时间"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        prop="timeLimit"
        label="截止时间"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        prop="useTime"
        label="耗时"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        prop="statusDesc"
        label="任务状态"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        prop="suspensionDesc"
        label="挂起"
        show-overflow-tooltip
        width="80"
      />
      <el-table-column
        prop="isReadDesc"
        label="已读"
        show-overflow-tooltip
        width="80"
      />
      <el-table-column
        prop="suspensionReason"
        label="挂起原因"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        prop="invalidReason"
        label="作废原因"
        show-overflow-tooltip
        width="100"
      >
        <template #default="scope">
          {{
            scope.row.runFlow && scope.row.runFlow.invalidReason
              ? scope.row.runFlow.invalidReason
              : ""
          }}
        </template>
      </el-table-column>
      <el-table-column
        prop="flowStatusDesc"
        label="流程状态"
        show-overflow-tooltip
        width="100"
      >
        <template #default="scope">
          {{
            scope.row.runFlow && scope.row.runFlow.flowStatusDesc
              ? scope.row.runFlow.flowStatusDesc
              : scope.row.flowInstId
          }}
        </template>
      </el-table-column>
      <el-table-column
        prop="parFlowInstId"
        label="父流程名称"
        show-overflow-tooltip
        width="160"
      >
        <template #default="scope">
          {{
            scope.row.parRunFlow && scope.row.parRunFlow.flowName
              ? scope.row.parRunFlow.flowName
              : scope.row.parFlowInstId
          }}
        </template>
      </el-table-column>
      <el-table-column
        prop="subDefFlowId"
        label="子流程名称"
        show-overflow-tooltip
        width="160"
      >
        <template #default="scope">
          {{
            scope.row.subRunFlow && scope.row.subRunFlow.flowName
              ? scope.row.subRunFlow.flowName
              : scope.row.subFlowInstId
          }}
        </template>
      </el-table-column>
      <el-table-column
        :label="translate('menus.operation')"
        width="80"
        fixed="right"
      >
        <template #default="scope">
          <div
            style="
              display: flex;
              align-items: center;
              justify-content: space-between;
            "
          >
            <el-button
              v-if="
                scope.row.suspension !== '1' &&
                (scope.row.status === '0' || scope.row.status === '9')
              "
              icon="Stamp"
              text
              type="primary"
              @click="handleJob(scope.row, true)"
            >
              审批
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="tableOptions.pagination.current"
      :layout="tableOptions.pagination.layout"
      :page-size="tableOptions.pagination.size"
      :total="tableOptions.pagination.total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />

    <CommonDialog
      v-if="data.showHandleJob"
      v-model="data.showHandleJob"
      title="办理任务"
      class="handle-job"
      :close-on-click-modal="true"
      fullscreen
      append-to-body
      :show-footer="true"
      :showFooterOk="false"
      cancelText="关闭"
      @cancel="data.showHandleJob = false"
      @closed="data.showHandleJob = false"
    >
      <HandleJob :curr-job="data.job" @complete="onHandleJobComplete" />
    </CommonDialog>
  </div>
</template>
<script setup lang="ts">
import { BasicTableProps, useTable } from "@/hooks/table";
import { useMessage } from "@/hooks/message";
import * as doJob from "@/api/flow/jobtask";
import { translate } from "@/plugins/i18n";
const HandleJob = defineAsyncComponent(
  () => import("@/components/flow/flowJobHandle.vue")
);

const tableRef = ref(null);

const data = reactive({
  showHandleJob: false,
  currJob: undefined,
  job: {
    id: "",
    orderInstId: "",
    defFlowId: "",
    flowInstId: "",
    flowKey: "",
    formId: "",
    flowName: "",
    runNodeId: "",
    runNodeStatus: "",
    defNodeId: "",
    defNodeFormMode: "normal",
    jobBtns: []
  }
});

const tableOptions: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {
    initiatorId: "",
    defFlowId: "",
    flowNodeId: "",
    nodeJobId: ""
  },
  autoLoad: true,
  queryFunction: doJob.fetchTodoPage,
  descs: ["start_time"]
});

const {
  tableStyle,
  queryTableData,
  handleSelectionChange,
  handleRowClick,
  handleSizeChange,
  handleCurrentChange,
  handleSortChange,
  handleToggleHeight
} = useTable(tableOptions, tableRef);

onMounted(() => {
  handleToggleHeight(200);
});

const handleJob = (row: any, saveRead?: boolean) => {
  const query = { id: row.id };
  doJob.getTodoDetail(query).then(response => {
    const todoDetail = response.data;
    data.currJob = response.data;
    data.job.id = data.currJob.id;
    data.job.jobBtns = data.currJob.jobBtns;
    data.job.runNodeId = data.currJob.runNodeId;
    data.job.runNodeStatus = data.currJob.runNode.status;
    data.job.orderInstId = todoDetail.runFlow
      ? todoDetail.runFlow.orderInstId
      : "";
    data.job.defFlowId = todoDetail.defFlowId;
    data.job.flowInstId = todoDetail.flowInstId;
    data.job.flowKey = todoDetail.flowKey;
    data.job.flowName = todoDetail.runFlow ? todoDetail.runFlow.flowName : "";
    data.job.formId = todoDetail.runFlow ? todoDetail.runFlow.formId : "";
    data.job.defNodeId = todoDetail.defNodeId;
    data.job.defNodeFormMode = todoDetail.defNodeFormMode;
    if ("1" === data.currJob.status) {
      useMessage().warning("当前任务已被其他办理人审批");
      queryTableData();
    } else {
      data.showHandleJob = true;
      if (saveRead) {
        doJob.isRead({ id: data.currJob.id }).then(() => {
          row.isRead = "1";
        });
      }
    }
  });
};

const onHandleJobComplete = () => {
  queryTableData();
  data.showHandleJob = false;
};

defineExpose({ queryTableData });
</script>
